import {
  call, put, all, takeLatest, select
} from 'redux-saga/effects'
import api from '@API'
import {
  GET_MY_MEDITATION_SUMMARY,
  GET_SHARABLE_MEDITATION,
  SHARE_MEDITATION,
  GET_MEDITATION_DETAIL,
  PLAY_MEDITATION_AUDIO,
  STOP_MEDITATION_AUDIO,
  GET_MEDITATION_INTRO,
  GET_MEDITATION_LIST,
  GET_TAG_MEDITATIONS,
} from '@ACTIONS/meditation'
import { UPDATE_PAIN_MANAGEMENT } from '@ACTIONS/managementPlan'
import { STORAGE_KEY } from '@CONSTANTS/common'
import Taro from '@tarojs/taro'
import {
  getMeditationDurationTime, getMeditationId,
} from './dataSelect'

const getMyMeditationSummary = function* getMyMeditationSummary(action) {
  try {
    const { IsToday = true } = action
    const data = yield call(api.getMyMeditationSummary)
    yield put({ type: GET_MY_MEDITATION_SUMMARY.SUCCESS, data })
    if (IsToday) {
      yield put({ type: UPDATE_PAIN_MANAGEMENT.REQUEST })
    }
  } catch (error) {
    yield put({ type: GET_MY_MEDITATION_SUMMARY.FAILURE, error })
  }
}

const playMeditationAudio = function* playMeditationAudio() {
  try {
    const IsToday = Taro.getStorageSync(STORAGE_KEY.meditation_is_today) !== false
    const MeditationId = yield select(getMeditationId)
    const Duration = yield select(getMeditationDurationTime)
    yield call(api.playMeditation, { MeditationId, Duration, IsToday })
    yield put({ type: PLAY_MEDITATION_AUDIO.SUCCESS })
  } catch (error) {
    yield put({ type: PLAY_MEDITATION_AUDIO.FAILURE, error })
  }
}

const stopMeditationAudio = function* stopMeditationAudio(action) {
  try {
    const { finished = false } = action
    const IsToday = Taro.getStorageSync(STORAGE_KEY.meditation_is_today) !== false
    const MeditationId = yield select(getMeditationId)
    yield call(api.stopMeditation, { MeditationId, Finished: finished, IsToday })
    yield put({ type: STOP_MEDITATION_AUDIO.SUCCESS, finished })
    yield put({ type: GET_MY_MEDITATION_SUMMARY.REQUEST, IsToday })
  } catch (error) {
    yield put({ type: STOP_MEDITATION_AUDIO.FAILURE, error })
  }
}

const shareMeditation = function* shareMeditation(action) {
  try {
    const { payload } = action
    const { share = true, ...params } = payload
    const data = yield call(api.shareMeditation, params)
    yield put({ type: SHARE_MEDITATION.SUCCESS, data, share })
  } catch (error) {
    yield put({ type: SHARE_MEDITATION.FAILURE, error })
  }
}

const getSharableMeditation = function* getSharableMeditation(action) {
  try {
    const { reset = false } = action
    const data = yield call(api.getSharableMeditation)
    yield put({ type: GET_SHARABLE_MEDITATION.SUCCESS, data, reset })
    if (data.Share) {
      yield put({ type: GET_MY_MEDITATION_SUMMARY.REQUEST })
    }
  } catch (error) {
    yield put({ type: GET_SHARABLE_MEDITATION.FAILURE, error })
  }
}

const getMeditationDetail = function* getMeditationDetail(action) {
  try {
    const { payload } = action
    const { mId } = payload
    const data = yield call(api.getMeditation, payload)
    yield put({ type: GET_MEDITATION_DETAIL.SUCCESS, data, meditationId: mId })
  } catch (error) {
    yield put({ type: GET_MEDITATION_DETAIL.FAILURE, error })
  }
}

const getMeditationIntro = function* getMeditationIntro() {
  try {
    const data = yield call(api.getEventsListBySpotCode, { code: 'MeditationIntro' })
    yield put({ type: GET_MEDITATION_INTRO.SUCCESS, data })
  } catch (error) {
    yield put({ type: GET_MEDITATION_INTRO.FAILURE, error })
  }
}

const getMeditationList = function* getMeditationList() {
  try {
    const data = yield call(api.getEventsListBySpotCode, { code: 'MeditationList' })
    yield put({ type: GET_MEDITATION_LIST.SUCCESS, data })
    if (Array.isArray(data) && data.length > 0) {
      const reqArr = data.map(({ RouteData: { mId, tId, id } }) => (
        call(api.getMeditations, { tagId: mId || tId || id })
      ))
      const list = yield all(reqArr)
      yield put({ type: GET_TAG_MEDITATIONS.SUCCESS, data: list.map(({ Meditations }) => Meditations) })
    }
  } catch (error) {
    yield put({ type: GET_MEDITATION_LIST.FAILURE, error })
  }
}

export default function* rootSaga() {
  yield all([
    takeLatest(GET_MY_MEDITATION_SUMMARY.REQUEST, getMyMeditationSummary),
    takeLatest(PLAY_MEDITATION_AUDIO.REQUEST, playMeditationAudio),
    takeLatest(STOP_MEDITATION_AUDIO.REQUEST, stopMeditationAudio),
    takeLatest(GET_SHARABLE_MEDITATION.REQUEST, getSharableMeditation),
    takeLatest(GET_MEDITATION_DETAIL.REQUEST, getMeditationDetail),
    takeLatest(SHARE_MEDITATION.REQUEST, shareMeditation),
    takeLatest(GET_MEDITATION_INTRO.REQUEST, getMeditationIntro),
    takeLatest(GET_MEDITATION_LIST.REQUEST, getMeditationList),
  ])
}

